package com.wanmait.bootdemo;

import com.wanmait.bootdemo.interceptor.JWTAuthcInterceptor;
import com.wanmait.bootdemo.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;
    @Resource
    private JWTAuthcInterceptor jwtAuthcInterceptor;
/*  springMVC.xml中配置方式：
    <mvc:interceptors>
        <mvc:interceptor>
            <bean class="com.wanmait.demo.interceptor.LoginInterceptor"/>
            <mvc:mapping path="/manage/**"/>
            <mvc:exclude-mapping path="/manage/login"/>
        </mvc:interceptor>
    </mvc:interceptors>
    */

    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/manage/**")
                .excludePathPatterns("/manage/login");
        registry.addInterceptor(jwtAuthcInterceptor)
                .addPathPatterns("/manage/**");
                //.excludePathPatterns("/manage/login");
    }

    /*//servlet中设置跨域
    *//* 允许跨域的主机地址 *//*
    response.setHeader("Access-Control-Allow-Origin", "*");
        *//* 允许跨域的请求方法GET, POST, HEAD 等 *//*
    response.setHeader("Access-Control-Allow-Methods", "*");
        *//* 重新预检验跨域的缓存时间 (s) *//*
    response.setHeader("Access-Control-Max-Age", "3600");
        *//* 允许跨域的请求头 *//*
    response.setHeader("Access-Control-Allow-Headers", "*");
        *//* 是否携带cookie *//*
    response.setHeader("Access-Control-Allow-Credentials", "true");  */

    //配置全局跨域资源共享,下面方法或者配置跨域过滤器方式使用其中一种即可
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //.allowedOrigins("http://www.wanmait.com","http://localhost:6372")
                .allowedOrigins("*")//允许哪些域名访问
                .allowedHeaders("*");//允许哪些请求头
    }
/*    @Bean
    public QuestionTypeService questionTypeService(){
        return new QuestionTypeServiceImpl();
    }*/

    //配置全局的跨域资源共享，跨域过滤器
/*    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfig = new CorsConfiguration();
        //corsConfig.addAllowedOrigin("http://www.wanmait.com");
        //corsConfig.addAllowedOrigin("http://localhost:6372");
        corsConfig.addAllowedOrigin("*");
        source.registerCorsConfiguration("/manage/**",corsConfig);
        CorsFilter corsFilter = new CorsFilter(source);
        return corsFilter;
    }*/

}
